<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/11/19
 * Time: 17:52
 */

namespace App\Services\BrandService;


use App\Services\ProductTempService\ProductTempTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Support\Facades\DB;
use Validator;
class BrandBackService
{
    protected $brandToolSer;
    /**
     * BrandBackService constructor.
     */
    public function __construct(BrandToolService $brandToolSer)
    {
        $this->brandToolSer=$brandToolSer;
    }

    /***
     * 获取品牌列表
     * @param array $data
     * @return mixed
     */
    public function getBrandList(array $data)
    {
        $per_page = isset($data['per_page'])?$data['per_page']:10;
        $brands = DB::table('brands')->where('state',1)
            ->orderBy('sort','desc')
            ->paginate($per_page);
        return $brands;
    }
    /***
     * 品牌删除
     * @param $brand_id
     * @return mixed
     */
    public function deleteBrand($brand_id){
        try {
            DB::beginTransaction();
            $num =  DB::table("brands")->where(array('brand_id'=>$brand_id,'state'=>1)) ->update(['state' => 0,'updated_at'=>time()]);
            if($num>0){
                $del_prod_temp_res=ProductTempTool::delProdTempByBrandId($brand_id);
            }
            return ($num>0)?true:false;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }

    /***
     * 品牌更新
     * @param array $data
     * @return bool
     */
    public function updateBrand(array  $data){
        try {
            DB::beginTransaction();
            $brand_id=$data['brand_id'];
            $data['updated_at']=time();
            $num =  DB::table("brands")->where(array('brand_id'=>$brand_id,'state'=>1)) ->update($data);
            return ($num>0)?true:false;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }
    public function saveBrand(array  $data){
        try {
            $brand_id=GeneralTool::only_no();
            $data['brand_id']=$brand_id;
            $data['updated_at']=time();
            $num =  DB::table("brands")->where(array('brand_id'=>$brand_id,'state'=>1)) ->insert($data);
            return ($num>0)?true:false;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    public function addValidator(array $brand)
    {
        $v = [
            'brand_name' => 'required|max:64',
            'description' => 'max:200',
            'logo'=>'required|image'
        ];
        return  Validator::make(
            $brand,$v

        );
    }
    /***
     *  查看品牌名是否已经使用
     * @param $brand_id
     * @param $brand_name
     * @return bool
     */
    public function getBrandForNameIsNotCurrent($brand_id,$brand_name){
        if(empty($brand_name)){
            return false;
        }
        $query =  DB::table('brands')->where(array('state'=>1,'brand_name'=>$brand_name));
        if(!empty($brand_id)){
            $query->where('brand_id',$brand_id);
        }
        $num=$query->count();
        return ($num>0)?true:false;
    }
}